package com.gm.controller.cardType;

import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.gm.common.Pager;
import com.gm.model.Base.Const;
import com.gm.model.po.CardType;
import com.gm.model.vo.ResMsg;
import com.gm.model.vo.ValidVo;
import com.gm.service.user.CardTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * Created by taojie on 17-6-21.
 */
@Controller
@RequestMapping("cardType/")
public class CardTypeController {

    private Logger logger = LoggerFactory.getLogger(CardTypeController.class);

    @Autowired
    private CardTypeService cardTypeService;

    @RequestMapping("cardTypeList.web")
    public ModelAndView rateList(Pager pager){
        ModelAndView view= new ModelAndView();
        PageList<CardType> cardTypeList = cardTypeService.getCardTypeList(pager);
        Paginator paginator =cardTypeList.getPaginator();
        view.addObject("cardTypeList", cardTypeList);
        view.addObject("paginator", paginator);
        view.setViewName("/cardType/cardType.jsp");
        return view;
    }

    @RequestMapping("gotoAddCardType.web")
    public ModelAndView gotoAddUser(){
        ModelAndView view= new ModelAndView();
        view.setViewName("/cardType/addCardType.jsp");
        return view;
    }

    @RequestMapping(value = "checkCardTypeOnly.do",method = RequestMethod.POST)
    @ResponseBody
    public ValidVo checkCardTypeOnly(@RequestParam(value = "name", required = true) String name, RedirectAttributes attr){
        logger.info("Method[checkCardTypeOnly],name[{}]",name);
        ValidVo validVo = new ValidVo();
        CardType cardType = cardTypeService.selectByName(name);
        if(cardType == null){
            validVo.setValid(true);
        }else{
            validVo.setValid(false);
        }
        logger.info("Method[checkCardTypeOnly],validVo.isValid[{}]",validVo.isValid());
        return validVo;
    }

    @RequestMapping(value = "addCardType.do", method = RequestMethod.POST)
    @ResponseBody
    public ResMsg addCardType(CardType cardType, RedirectAttributes attr){
        logger.info("Method[addUser],cardType.getName[{}]",cardType.getName());
        ResMsg resMsg = new ResMsg();
        int ret = cardTypeService.addCardType(cardType);
        if(ret==1){
            resMsg.setStatus(Const.RES_OK);
        }else{
            resMsg.setError("添加失败");
        }
        logger.info("Method[addUser],ret[{}]",ret);
        return resMsg;
    }

    @RequestMapping("gotoUpdateCardType.web")
    public ModelAndView gotoUpdateUser(Integer cardTypeId,Integer currentPage){
        ModelAndView view= new ModelAndView();
        CardType cardType = cardTypeService.selectByPrimaryKey(cardTypeId);
        view.addObject("currentPage", currentPage);
        view.addObject("cardType", cardType);
        view.setViewName("/cardType/updateCardType.jsp");
        return view;
    }

    @RequestMapping(value = "updateCardType.do", method = RequestMethod.POST)
    @ResponseBody
    public ResMsg updateCardType(CardType cardType,Integer currentPage,RedirectAttributes attr){
        logger.info("Method[updateCardType],cardType.getName[{}]",cardType.getName());
        ResMsg resMsg = new ResMsg();
        int ret = cardTypeService.updateCardType(cardType);
        if(ret == 1){
            resMsg.setStatus(Const.RES_OK);
        }else{
            resMsg.setError("修改失败");
        }
        logger.info("Method[updateCardType],ret[{}]",ret);

        return resMsg;
    }



    @RequestMapping("deleteCardType.web")
    public ModelAndView deleteUser(Integer rateId,Integer currentPage,RedirectAttributes attr){
        ModelAndView view= new ModelAndView();
        int ret = cardTypeService.deleteCardType(rateId);
        if(ret==1){
            attr.addFlashAttribute("msg", "删除成功");
        }else{
            attr.addFlashAttribute("msg", "删除失败");
        }
        Pager pager = new Pager();
        pager.setCurrentPage(currentPage);
        List<CardType> userList = cardTypeService.getCardTypeList(pager);
        if(userList.size() == 0){
            currentPage = currentPage -1;
        }
        if(currentPage==0)currentPage=1;
        view.addObject("currentPage", currentPage);
        view.setViewName("redirect:/cardType/cardTypeList.web");
        return view;
    }
}
